package com.rritw.webtools.datasource;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MultipleDataSourceAspectAdvice {

    /**
     * 多数据源自动切换，根据不同的Mapper，选择不同的数据源
     * 
     * @param jp
     * @return
     * @throws Throwable
     */
    @Around("execution(* com.keepc..mapper.*TMapper.*(..))")
    public Object doAround(ProceedingJoinPoint jp) throws Throwable {
        Signature signature = jp.getSignature();
        if (signature instanceof MethodSignature) {
            // jp.getSignature().getName() method name
        }

        if (jp.getTarget() instanceof Object) {
            MultipleDataSource.setDataSourceKey("im.dataSource");
        } else {
            MultipleDataSource.setDataSourceKey("ips.dataSource");
        }
        return jp.proceed();
    }
}
